<?php 

Class appModel extends Model
{
	public function get_app()
	{
		$arr=array();
		$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='appid' OR op_key='appsecret'";
		$temp=$this->getAll($sql);
		$arr['appid']=$temp[0]['op_value'];
		$arr['appsecret']=$temp[1]['op_value'];
		return $arr;
	}
	public function is_member()
	{
	
		if ((!isset($_SESSION['user_id']) || intval($_SESSION['user_id']) <= 0) &&
		ACTION_NAME!= 'login' )
		{
			/* session 不存在，检查cookie */
			if (!empty($_COOKIE['QIAQIA']['user_id']) && !empty($_COOKIE['QIAQIA']['user_pwd']))
			{
				// 找到了cookie, 验证cookie信息
				$sql = 'SELECT id, phone, password' .
						' FROM ' .$this->table('users') .
						" WHERE id = '" . intval($_COOKIE['QIAQIA']['user_id']) . "'";
				$row = $this->getRow($sql);
	
				if (!$row)
				{
					// 没有找到这个记录
					cookie($_COOKIE['QIAQIA']['user_id'],   '');
					cookie($_COOKIE['QIAQIA']['userpwd'], '');
	
					return false;
				}
				else
				{
					// 检查密码是否正确
					if (md5($row['password']) == $_COOKIE['QIAQIA']['userpwd'])
					{
	
	
	
						session('user_id',$row['id']);
						session('admin_name',$row['phone']);
						return true;
					}
					else
					{
						cookie($_COOKIE['QIAQIA']['user_id'],   '');
						cookie($_COOKIE['QIAQIA']['userpwd'], '');
	
						return false;
					}
				}
			}
			else
			{
				return false;
			}
		}
		else
		{
			return true;
		}
			
	}
	public function get_menu(){
		$sql="SELECT id,name,type,code,redirect_url as url,is_extend_links FROM ".$this->table('category')." WHERE is_show = 1 AND parent_id=0 ORDER BY ordering DESC";
		$arr=$this->getAll($sql);
		foreach($arr as $k=>$v)
		{
			$sql="SELECT id,name,type,is_extend_links,redirect_url,code FROM ".$this->table('category')." WHERE is_show = 1 AND parent_id=".$v['id']." ORDER BY ordering DESC";
			if($_GET['code']==$v['code'])
			{
				$arr[$k]['current']=1;
			}
			
			$arr[$k]['subchild']=$this->getAll($sql);
			
		}
		
		foreach($arr as $k=>$v)
		{
				$arr[$k]['name']=hy_dig_filter($v['name']);
				if($v['is_extend_links']==1)
				{
					
					$arr[$k]['url']=$v['redirect_url'];
					
				}
				else 
				{
					if(REWRITE==1)
					{
						switch($v['type'])
						{
							case 1:$arr[$k]['url']="/".$v['code']."/article/index.html";break;
							case 2:$arr[$k]['url']="/".$v['code']."/article/photo.html";break;
							case 3:$arr[$k]['url']="/".$v['code']."/article/page.html";break;
							case 4:$arr[$k]['url']="/goods/list.html?id=".$v['id'];break;
							
						}
					}
					else 
					{
					  switch($v['type'])
					  {
						case 1:$arr[$k]['url']="index.php?g=home&m=article&a=index&code=".$v['code'];break;
						case 2:$arr[$k]['url']="index.php?g=home&m=article&a=photo&code=".$v['code'];break;
						case 3:$arr[$k]['url']="index.php?g=home&m=article&a=page&code=".$v['code'];break;
						case 4:$arr[$k]['url']="index.php?g=home&m=goods&a=glist&id=".$v['id'];break;
					
					  }
					}
				
				}
		   	if($v['subchild'])
			{
				foreach($v['subchild'] as $kk=>$vv)
				{
					$arr[$k]['subchild'][$kk]['name']=hy_dig_filter($vv['name']);
					if($_GET['code']==$vv['code'])
					{
						$arr[$k]['current']=1;
					}
					if($vv['is_extend_links']==1)
					{
						$arr[$k]['subchild'][$kk]['url']=$vv['redirect_url'];
					}
					else
					{
						if(REWRITE==1)
						{
							switch($vv['type'])
							{
								case 1:$arr[$k]['subchild'][$kk]['url']="/".$vv['code']."/article/index.html";break;
								case 2:$arr[$k]['subchild'][$kk]['url']="/".$vv['code']."/photo/index.html";break;
								case 3:$arr[$k]['subchild'][$kk]['url']="/".$vv['code']."/article/page.html";break;
								case 4:$arr[$k]['subchild'][$kk]['url']="/goods/list.html?id=".$vv['id'];break;
							}
						}
						else 
						{
						  switch($vv['type'])
						  {
							case 1:$arr[$k]['subchild'][$kk]['url']="index.php?g=home&m=article&a=index&code=".$vv['code'];break;
							case 2:$arr[$k]['subchild'][$kk]['url']="index.php?g=home&m=photo&a=index&code=".$vv['code'];break;
							case 3:$arr[$k]['subchild'][$kk]['url']="index.php?g=home&m=article&a=page&code=".$vv['code'];break;
							case 4:$arr[$k]['subchild'][$kk]['url']="index.php?g=home&m=goods&a=glist&id=".$vv['id'];break;
						  }
						}
					
					}
				}
				
			}
			if($v['url']==''||$v['url']=='http://')
			{
				$arr[$k]['url']=$arr[$k]['subchild'][0]['url'];
			}
			
		   	
		}
		return $arr;
	} 
	public function current_menu($id)
	{
		$arr=array();
		if($id!='')
		{
		   $sql="SELECT name,keywords,description FROM ".$this->table('category')." WHERE id=".$id;

		   $arr=$this->getRow($sql);
		   $arr['name']=hy_dig_filter($arr['name']);
		 
		}
		return $arr;
	}
	//遍历上级所有分类
	private function find_parent_cat($child_id)
	{
		static $arr;
		$sql="SELECT parent_id,name,id,code,type,redirect_url FROM ".$this->table('category')." WHERE id = '".$child_id."'";
		$temp=$this->getRow($sql);
		$temp['name']=hy_dig_filter($temp['name']);
		if(REWRITE==1)
		{
			switch($temp['type'])
			{
				case 1:$temp['url']="/".$temp['code']."/article/index.html";break;
				case 2:$temp['url']="/".$temp['code']."/article/photo.html";break;
				case 3:$temp['url']="/".$temp['code']."/article/page.html";break;
				case 4:$temp['url']="/goods/list.html?id=".$temp['id'];break;
				default :$temp['url']=$temp['redirect_url']?$temp['redirect_url']:'#';
			}
		}
		else
		{
			switch($temp['type'])
			{
				case 1:$temp['url']="index.php?g=home&m=article&a=index&code=".$temp['code'];break;
				case 2:$temp['url']="index.php?g=home&m=article&a=photo&code=".$temp['code'];break;
				case 3:$temp['url']="index.php?g=home&m=article&a=page&code=".$temp['code'];break;
				case 4:$temp['url']="index.php?g=home&m=goods&a=glist&id=".$temp['id'];break;
				default :$temp['url']=$temp['redirect_url']?$temp['redirect_url']:'#';
			}
		}
		$arr[]=$temp;
		
		if($temp['parent_id']==0)
		{
			$temp2=$arr;
			$arr=array();
			return $temp2;
		}
		else {
		return $this->find_parent_cat($temp['parent_id']);
		}
		
	}
	public function ur_here($args)
	{
		$model=MODULE_NAME;
		$action=ACTION_NAME;
		$id=isset($_GET['id'])?intval($_GET['id']):'';
		$code=isset($args['code'])?$args['code']:$_GET['code'];
		$urhere="";
	   if($model=="article"&&$action=="view")
		{ 
		    if($id)
			{
				$sql="SELECT cat_id,title FROM ".$this->table('article')." WHERE id = '".$id."'";
				$goods=$this->getRow($sql);
				
				$cat=$this->find_parent_cat($goods['cat_id']);
				krsort($cat);
				foreach($cat as $k=>$v)
				{
					$urhere.='<a href="'.$v['url'].'">'.$v['name'].'</a>>';
				}
				//$urhere.=hy_dig_filter($goods['title']);
				$urhere=substr($urhere,0,strlen($urhere)-1);
			}
		}
		
		elseif($model=="goods"&&$action=="view")
		{
			if($id)
			{
				$sql="SELECT cat_id,goods_name FROM ".$this->table('goods')." WHERE id = '".$id."'";
				$goods=$this->getRow($sql);
				
				$cat=$this->find_parent_cat($goods['cat_id']);
				krsort($cat);
				foreach($cat as $k=>$v)
				{
					$urhere.='<a href="'.$v['url'].'">'.$v['name'].'</a>>';
				}
				//$urhere.=hy_dig_filter($goods['goods_name']);
				$urhere=substr($urhere,0,strlen($urhere)-1);
			}
		}
		else
		{
			if($code)
			{
			$sql="SELECT name,id FROM ".$this->table('category')." WHERE code = '".$code."'";
			}
			else 
			{
				$sql="SELECT name,id FROM ".$this->table('category')." WHERE id = '".$id."'";
			}
			$goods=$this->getRow($sql);
		
			$cat=$this->find_parent_cat($goods['id']);
			krsort($cat);
			foreach($cat as $k=>$v)
			{
				$urhere.='<a href="'.$v['url'].'">'.$v['name'].'</a>>';
			}
			$urhere=substr($urhere,0,strlen($urhere)-1);
		}
		echo $urhere;
		return $urhere;
	}
	//获取最顶级分类
	public function getTopClass($id)
	{
		static $res; 
		if(!ctype_digit($id))
		{
			$sql="SELECT code,id,parent_id FROM ".$this->table('category')." WHERE code='".$id."'";
		}
		else 
		{
		$sql="SELECT code,id,parent_id FROM ".$this->table('category')." WHERE id='".$id."'";
		}
		$res=$this->getRow($sql);
	    if($res['parent_id']!=0)
	    {
	    	$this->getTopClass($res['parent_id']);
	    }
	    
		return $res;
	}
	//获取父分类
	public function getPClass($id)
	{
		$sql="SELECT p.code,p.id FROM ".$this->table('category')." as p,".$this->table('category')." as c WHERE c.parent_id=p.id AND c.id='".$id."'";
		$res=$this->getRow($sql);
		
		return $res;
	}
	//获取现在分类
	public function getCurClass($id)
	{
		$sql='SELECT code,id,name,thumb,type FROM '.$this->table('category')." WHERE id='".$id."'";
		$res=$this->getRow($sql);
		if($res)
		{
			$res['ch_name']=hy_dig_filter($res['name'],1);
			$res['en_name']=hy_dig_filter($res['name'],2);
				$res['name']=hy_dig_filter($res['name']);
				if(REWRITE==1)
				{
					switch($res['type'])
					{
						case 1:$parent['url']="/".$res['code']."/article/index.html";break;
					    case 2:$parent['url']="/".$res['code']."/article/photo.html";break;
					    case 3:$parent['url']="/".$res['code']."/article/page.html";break;
					}
				}
				else
				{
			    	switch($res['type'])
				  {
					case 1:$parent['url']="index.php?g=home&m=article&a=index&code=".$res['code'];break;
					case 2:$parent['url']="index.php?g=home&m=article&a=photo&code=".$res['code'];break;
					case 3:$parent['url']="index.php?g=home&m=article&a=page&code=".$res['code'];break;
				
				  }
				}
				
			
		}
	
		return $res;
	}
	//获取分类下所有的子分类
	public function getCatTree($arrCat, $parent_id = 0, $level = 0)
	{
		static $arrTree =array(); 
		if($level==0)
		{
			$arrTree=array();
		}
	     $sql='SELECT id,parent_id FROM '.$this->table('category')." WHERE parent_id='".$parent_id."'";
	     $arrCat=$this->getAll($sql);
	     $level++;
	    if(empty($arrCat))
	    {
	    	return false;
	    }
	    foreach($arrCat as $key => $value)
	    {
	    	$arrTree[] = $value['id']; 
	    	$this->getCatTree($arrCat, $value['id'],$level);
	    }
		 return array_flip(array_flip($arrTree));
	}
	//获取子分类
	public function getSubClass($id)
	{
		$sql='SELECT code,id,name,thumb,type FROM '.$this->table('category')." WHERE parent_id='".$id."'";
		$res=$this->getAll($sql);
		if($res)
		{
		foreach($res as $k=>$v)
		{
			$res[$k]['name']=hy_dig_filter($v['name'],1);
			$res[$k]['en_name']=hy_dig_filter($v['name'],2);
			if(REWRITE==1)
			{
			     switch($v['type'])
				{
					    case 1:$parent['url']="/".$v['code']."/article/index.html";break;
					    case 2:$parent['url']="/".$v['code']."/article/photo.html";break;
					    case 3:$parent['url']="/".$v['code']."/article/page.html";break;
				}
			}
			else
			{
				
				switch($v['type'])
				{
					case 1:$parent['url']="index.php?g=home&m=article&a=index&code=".$v['code'];break;
					case 2:$parent['url']="index.php?g=home&m=article&a=photo&code=".$v['code'];break;
					case 3:$parent['url']="index.php?g=home&m=article&a=page&code=".$v['code'];break;
				}
			}
			
		}
		}
		
		return $res;
	}
    public function code_to_id($code)
    {
    	$sql='SELECT id FROM '.$this->table('category')." WHERE code='".$code."'";
    	return $this->getOne($sql);
    }
	public function get_ad($opt)
	{
		$option=array();
		$sql="SELECT id FROM ".$this->table('block')." WHERE code ='".$opt['code']."'";
		$flash_id=$this->getOne($sql);
		if($flash_id)
		{
		$sql="SELECT weight,height FROM ".$this->table('block')." WHERE id ='".$flash_id."'";
		$flash_cfg=$this->getRow($sql);
		
		$sql="SELECT * FROM ".$this->table('block_content')." WHERE block_id ='".$flash_id."' ORDER BY ordering DESC";
		$flash_img=$this->getAll($sql);
		
		
		  foreach($flash_img as $k=>$v)
		  {
		  	$flash_img[$k]['ch_title']=hy_dig_filter($v['title'],1);
		  	$flash_img[$k]['en_title']=hy_dig_filter($v['title'],2);
		  	$flash_img[$k]['title']=hy_dig_filter($v['title']);
		  	$flash_img[$k]['description']=hy_dig_filter(str_replace(PHP_EOL,'<br>', $v['description']));
		  }
		}
	     return $flash_img;
	}
	public function get_cfg($key='')
	{
		if($key)
		{
			$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='".$key."'";
			$arr=$this->getOne($sql);
			
		}
		else 
		{
		   $sql="SELECT * FROM ".$this->table('options')." ";
		   $arr=array();
		   $res=$this->getAll($sql);
		   
		   if(count($res)>0)
		  {
			  foreach($res as $k=>$v)
			  {
				$arr[$v['op_key']]=$v['op_value'];
			  }
		   }
		}
		
		return $arr;
	}
}